<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/21
 * Time: 13:39
 */

namespace app\index\controller;

use think\Config;
use think\Controller;
use think\Request;

use OSS\OssClient;
use OSS\Core\OssException;

class Osstest extends Controller
{
    /**
     * @return Request
     */


    public function _initialize()
    {
        $accessKeyId = config('oss_config.KEY_ID');
        $accessKeySecret =  config('oss_config.KEY_SECRET');
        $endpoint = config('oss_config.END_POINT');
        $bucket= config('oss_config.BUCKET');

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        } catch (OssException $e) {
            print $e->getMessage();
        }

    }


    /**
     * @return View
     */
    public function ossform()
    {
        return $this->fetch('form');
    }

    public function index()
    {
        $accessKeyId = config('oss_config.KEY_ID');
        $accessKeySecret =  config('oss_config.KEY_SECRET');
        $endpoint = config('oss_config.END_POINT');
        $bucket= config('oss_config.BUCKET');
        $object = "13/cs2";
        $content = "oss测试1.";
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->putObject($bucket, $object, $content);
        } catch (OssException $e) {
            return $e->getMessage();
        }

    }
    public function upimg()
    {
        $accessKeyId = config('oss_config.KEY_ID');
        $accessKeySecret =  config('oss_config.KEY_SECRET');
        $endpoint = config('oss_config.END_POINT');
        $bucket= config('oss_config.BUCKET');
        $file = request()->file('file');


        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $object ='jpt'.'/'. $info->getFilename();
            $filePath = ROOT_PATH . 'public' . DS . 'uploads'.DS.$info->getSaveName();
            try{
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                $ossClient->uploadFile($bucket, $object, $filePath);
            } catch(OssException $e) {
                printf(__FUNCTION__ . ": FAILED\n");
                printf($e->getMessage() . "\n");
                return;
            }
            $ossurl='http://oss.ytclub.top';
            $ur=$ossurl.'/'.$object;
            unlink(ROOT_PATH . 'public' . DS . 'uploads'.DS.$info->getSaveName());

return json(['保存外链'=>$ur ]);
        }else{
            return '1';
        }





    }
}